package dproxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @author 761472239
 * @Classname DProxy
 * @Description TODO
 * @Date 2021/9/1 23:52
 */
public class DProxy implements InvocationHandler {
    private Subject subject;

    public Subject getSubject(Subject subject){
        this.subject = subject;
        Class clazz = subject.getClass();
        return (Subject) Proxy.newProxyInstance(clazz.getClassLoader(),clazz.getInterfaces(),this);
    }
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        before();
        Object result = method.invoke(this.subject, args);
        after();
        return result;
    }

    private void before() {
        System.out.println("动态代理，预处理");
    }

    private void after() {
        System.out.println("动态代理，后续处理");
    }
}
